package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    StudentService studentService = new StudentService();
    Scanner sc = new Scanner(System.in);

    public void strat() {
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //添加
                    addStudent();
                    break;
                case "2":
                    //删除
                    deleteStudent();
                    break;
                case "3":
                    //修改
                    updataStudent();
                    break;
                case "4":
                    //查看
                    findAllStudent();
                    break;
                case "5":
                    //退出
                    System.out.println("感谢使用学生管理系统,再见!");
                    return;
                default:
                    System.out.println("您输入有误,请重新输入");
            }
        }

    }

    public void updataStudent() {
        System.out.println("请输入您要修改的学号:");
        String id;
        while (true) {
            id = sc.nextLine();
            boolean exists = studentService.findStudent(id);
            if (exists){
                break;
            }else{
                System.out.println("您输入的学号不存在,请重新输入:");
            }
        }
        System.out.println("请输入您要修改的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入您要修改的年龄:");
        String age = sc.nextLine();
        System.out.println("请输入您要修改的生日:");
        String birthday = sc.nextLine();

        Student student = new Student(id, name, age, birthday);
        studentService.updataStudent(id,student);
        System.out.println("修改成功!");
    }

    public void deleteStudent() {
        System.out.println("请输入您要删除的学号:");
        String id;
        while (true) {
            id = sc.nextLine();
            boolean exists = studentService.findStudent(id);
            if (exists){
                studentService.deleteStudent(id);
                System.out.println("删除成功!");
                break;
            }else{
                System.out.println("您输入的学号不存在,请重新输入:");
            }
        }

    }

    public void findAllStudent() {
        Student[] stu = studentService.findAllStudent();
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            if (student != null) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }
    }

    public void addStudent() {
        String id;
        System.out.println("请输入您要添加的学号:");
        while (true) {
            id = sc.nextLine();
            boolean exists = studentService.findStudent(id);
            if (!exists) {
                break;
            } else {
                System.out.println("你输入的学号已经存在,请重新输入:");
            }
        }
        System.out.println("请输入您要添加的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入您要添加的年龄:");
        String age = sc.nextLine();
        System.out.println("请输入您要添加的生日:");
        String birthday = sc.nextLine();

        Student student = new Student(id, name, age, birthday);
        studentService.addStudent(student);
        System.out.println("添加成功!");
    }
}
